var token = localStorage.getItem("token");

var itemID = "";
var itemCode = "";

var pageflag = "";
var sid = "";
var itemCode = "";
var funName = "";
var type = "";

(function(e) {

	setPxPerRem();
	mui.plusReady(function() {
		itemID = getQueryString("itemID");
		itemCode = getQueryString("itemCode");
		sid = getQueryString("sid");
		type = getQueryString("type");
		pageflag = getQueryString("pageflag");
		if(!isEmpty(itemCode)) {
			$("#code").html(itemCode);
		}
		if(isHaveNet()) {
			getWarehouseInInfo(itemID);
		} else {
			mui.toast("请检查网络连接");
		}
	});
})(mui);
var count = 0;

function getWarehouseInInfo(itemId) {
	token = localStorage.getItem("token");
	if(isEmpty(token)) {
		mui.toast("登录认证失败，请退出后重试");
		return;
	}
	var p = {
		op: "bill_content",
		token: token,
		data: {
			id: itemId,
		}
	};

	if(!isEmpty(sid)) {
		p = {
			op: "bill_content_rt",
			token: token,
			data: {
				sid: sid,
				tid: itemId
			}
		}
	}

	var jsonData = {
		p: JSON.stringify(p)
	}

	if(checkParams(jsonData)) {
		mui.toast("查询失败！");
		return;
	}

	var opts = {
		url: '',
		addressUrl: "testline",
		requestType: "GET",
		pastDate: jsonData
	}

	crossDomainAjax(opts, function(data) {
		plus.nativeUI.closeWaiting();
		if(!isEmpty(data)) {
			var rows = data.items;
			if(!isEmpty(sid)) {
				rows = data;
			}
			count = 0;
			for(var i = 0; i < rows.length; i++) {
				createItem(rows[i], i);
			}
			
			var heoght = $("#list").height();
			$("#countMeony").css("height" , heoght);
			$("#countMeony").css("paddingTop" , heoght/2 - 10);
			$("#countMeony").html("￥" + count);
		} else {
			mui.toast("暂无数据");
		}
	});
}

var insertDiv = document.getElementById("insertDiv");

function createItem(row, index) {
	var createLi = document.createElement("li");
	createLi.id = row.id;

	var id = row.id;
	var SM_Name = row.trade_no;
	var SM_Code = type;
	var SM_Spec = row.name;
	var SP_Place = row.amount;

	if(!isEmpty(sid)) {
		id = index + 1;
		SM_Name = row.sm_nbcode;
		SM_Code = type;
		SM_Spec = row.bof_name;
		SP_Place = row.shuiMoney;
	}
	count = Math.round(SP_Place) + count;
	createLi.innerHTML = '<cite style="width: 1.3rem;">' + id + '</cite>' +
		'<cite style="width: 3rem;">' + SM_Name + '</cite>' +
		'<cite style="width: 2rem;">' + SM_Code + '</cite>' +
		'<cite style="width: 2rem;">' + SM_Spec + '</cite>' +
		'<cite style="width: 2.6rem;">' + SP_Place + '</cite>';
	document.body.appendChild(createLi);
	insertBefore(createLi, insertDiv);
}